import { defineStore } from "pinia";
import { reactive, computed } from "vue";
import { Local } from '@/utils/storage';
import { STORE_CONFIG } from '@/stores/constant/cacheKey'

/** 配置信息 */
export const useConfigStore = defineStore('config', () => {

    let configModel = reactive<configModel>({});

    /** 配置 */
    const config = computed(() => {
        let storage = Local.get(STORE_CONFIG);
        if (storage) {
            configModel = storage;
        }
        return configModel;
    })

    /** 更新配置 */
    const saveConfig = () => {
        Local.set(STORE_CONFIG, configModel);
    }

    /**
     * 设置菜单激活项
     * @param {string} menu 菜单信息
     */
    const setMenuActive = (menu: string) => {
        let info = config;
        info.value.menuActive = menu;
        saveConfig();
    }

    return {
        config,

        setMenuActive
    };
});

/** 配置模型 */
interface configModel {
    /** 菜单激活项 */
    menuActive?: string
}